package com.spic.system.api.factory;

import com.spic.common.core.domain.R;
import com.spic.system.api.RemoteFileService;
import com.spic.system.api.domain.FileDomain;
import com.spic.system.api.domain.SysFile;
import com.spic.system.api.dto.DownloadFileDTO;
import com.spic.system.api.dto.SysFileRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 文件服务降级处理
 *
 * @author ruoyi
 */
@Component
public class RemoteFileFallbackFactory implements FallbackFactory<RemoteFileService>
{
    private static final Logger log = LoggerFactory.getLogger(RemoteFileFallbackFactory.class);

    @Override
    public RemoteFileService create(Throwable throwable)
    {
        log.error("文件服务调用失败:{}", throwable.getMessage());
        return new RemoteFileService()
        {
            @Override
            public R<SysFile> upload(MultipartFile file)
            {
                return R.fail("上传文件失败:" + throwable.getMessage());
            }

            @Override
            public R<String> uploadOfFilePath(FileDomain file) {
                return R.fail("上传文件失败:" + throwable.getMessage());
            }

            @Override
            public R<Map<String, SysFileRecord>> getFileRecord(List<String> ids) {
                return R.fail("获取文件urL失败:" + throwable.getMessage());
            }

            @Override
            public R<Map<String, SysFileRecord>> downloadfilelistCache(DownloadFileDTO downloadFileDTO) {
                return R.fail("导出附件到本地失败:" + throwable.getMessage());
            }

            @Override
            public R<String> getSysFileRecordByOriginalUrl(String originalUrl)
            {
                return R.fail("获取pdf转换后的url的地址失败:" + throwable.getMessage());
            }

            @Override
            public R<String> getwatermarkImageUrl(String url, String text) {
                return R.fail("获取违章图片加水印后的url地址失败:" + throwable.getMessage());
            }

            @Override
            public R<SysFileRecord> getFileByUrl(String url) {
                return R.fail("通过url获取文件信息失败:" + throwable.getMessage());
            }

        };
    }
}
